//
//  EFCouponViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/7.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFCouponViewController.h"
#import "EFCouponSegmentView.h"
#import "EFCouponViewItem.h"
#import "EFCouponViewCell.h"
#import "iToast.h"

@interface EFCouponViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong,nonatomic)EFCouponSegmentView* segmentView;
@property(nonatomic, strong)UIScrollView *scrollView;

@property(strong,nonatomic)NSMutableArray* dataArray1;
@property(strong,nonatomic)UITableView* tableView1;

@property(strong,nonatomic)NSMutableArray* dataArray2;
@property(strong,nonatomic)UITableView* tableView2;

@property(assign,nonatomic)NSInteger type;   //0默认  1订单选择
@property(strong,nonatomic)NSString* totalPrice;   //  1订单选择  总价格


@end

@implementation EFCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"优惠券";
        self.dataArray1 = [NSMutableArray array];
        self.dataArray2 = [NSMutableArray array];
        self.type = 0 ;
    }
    
    return self;
}

-(instancetype)initWithType:(NSInteger)type totlaPrice:(NSString *)totalPrice
{
    self = [self init];
    if(self){
        self.type = type;
        self.totalPrice = totalPrice;
    }
    return self;
}

-(void)initView{
    __weak typeof(self) weakSelf = self;
    if(self.type == 0)
    {
        [self.segmentView setSegmentArray:@[@"可用",@"不可用"] SelectIndex:^(NSInteger index) {
            [weakSelf clickIndex:index];
        }];
        [self.view addSubview:self.segmentView];
        [self.scrollView addSubview:self.tableView1];
        [self.scrollView addSubview:self.tableView2];
        
        [self.view addSubview:self.scrollView];
        
        [self.tableView1 addLegendHeaderWithRefreshingBlock:^{
            [weakSelf.dataArray1 removeAllObjects];
            [weakSelf requestCouponOnline];
            
        }];
        
        [self.tableView2 addLegendHeaderWithRefreshingBlock:^{
            [weakSelf.dataArray2 removeAllObjects];
            [weakSelf requestCouponOutOfDate];
            
        }];
        
        [self requestCouponOutOfDate];
        [self clickIndex:0];
    }
    else
    {
        [self.segmentView setSegmentArray:@[@"可用"] SelectIndex:^(NSInteger index) {
            [weakSelf clickIndex:index];
        }];
        [self.view addSubview:self.segmentView];
        [self.scrollView addSubview:self.tableView1];
        
        [self.view addSubview:self.scrollView];
        
        [self.tableView1 addLegendHeaderWithRefreshingBlock:^{
            [weakSelf.dataArray1 removeAllObjects];
            [weakSelf requestCouponOnline];
            
        }];
        [self clickIndex:0];
    }

}

- (void)clickIndex:(NSInteger)index{
    [UIView animateWithDuration:0.3f animations:^{
        self.scrollView.contentOffset = CGPointMake(index *kScreenWidth, 0);
    } completion:nil];
    switch (index) {
        case 0:{
            if (self.dataArray1.count == 0) {
                [self requestCouponOnline];
            }
            break;
        }
        case 1:{
            if (self.dataArray2.count == 0) {
                [self requestCouponOutOfDate];
            }
            break;
        }
        default:
            break;
    }
    
}

- (void)requestCouponOnline{
    NSString* page = @"1";
    NSString* size = @"10";
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    if(self.type == 0)
    {
        [APP_CONTEXT.net getCoupons:page size:size status:@"0" OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            [weakSelf.dataArray1 removeAllObjects];
            [weakSelf.tableView1.header endRefreshing];
            [self.HUD hide:YES];
            if(code == 0){
                if ([info objectForKey:@"list"]) {
                    NSArray *array = [info objectForKey:@"list"];
                    for (NSDictionary *dic in array) {
                        EFCouponViewItem *item = [EFCouponViewItem yy_modelWithDictionary:dic];
                        if(weakSelf.type == 1){
                            item.fromOrder = true;
                        }
                        if(item.status == 0){
                            [weakSelf.dataArray1 addObject:item];
                        }
                    }
                    [weakSelf.tableView1 reloadData];
                }
            }else{
                [[iToast makeText:message]show];
            }
        }];
    }
    else
    {
        [APP_CONTEXT.net getCouponLists:page size:size totalPrice:self.totalPrice OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            [weakSelf.dataArray1 removeAllObjects];
            [weakSelf.tableView1.header endRefreshing];
            [self.HUD hide:YES];
            if(code == 0){
                if ([info objectForKey:@"list"]) {
                    NSArray *array = [info objectForKey:@"list"];
                    for (NSDictionary *dic in array) {
                        EFCouponViewItem *item = [EFCouponViewItem yy_modelWithDictionary:dic];
                        if(weakSelf.type == 1){
                            item.fromOrder = true;
                        }
                        if(item.status == 0){
                            [weakSelf.dataArray1 addObject:item];
                        }
                    }
                    [weakSelf.tableView1 reloadData];
                }
            }else{
                [[iToast makeText:message]show];
            }
        }];
    }
    
}

- (void)requestCouponOutOfDate{
    NSString* page = @"1";
    NSString* size = @"10";
    __weak typeof(self) weakSelf = self;
    [APP_CONTEXT.net getCoupons:page size:size status:@"1" OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.dataArray2 removeAllObjects];
        [weakSelf.tableView2.header endRefreshing];
        if(code == 0){
            Boolean hasData = false;
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                for (NSDictionary *dic in array) {
                    EFCouponViewItem *item = [EFCouponViewItem yy_modelWithDictionary:dic];
                    if(item.status == 2){
                        hasData = true;
                        [weakSelf.dataArray2 addObject:item];
                    }
                }
                [weakSelf.tableView2 reloadData];
            }
//            if(!hasData){
//                [weakSelf showEmptyImageView:@"" message:@"暂无优惠券"];
//            }else{
//                [weakSelf hideEmptyView];
//            }
        }else{
            [[iToast makeText:message]show];
        }
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableView1) {
        if( self.type == 1){
            EFCouponViewItem  *item = (EFCouponViewItem*)self.dataArray1[indexPath.row];
            [[NSNotificationCenter defaultCenter] postNotificationName:IC_CHANGE_COUPON_NOTIFICATION object:item.id];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView1) {
        return self.dataArray1.count;
    }else if (tableView == self.tableView2) {
        return self.dataArray2.count;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }else if (tableView == self.tableView2) {
        item =  self.dataArray2[indexPath.row];
    }
    
    return item.viewHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ICTableViewItem *item;
    if (tableView == self.tableView1) {
        item =  self.dataArray1[indexPath.row];
    }else if (tableView == self.tableView2) {
        item =  self.dataArray2[indexPath.row];
    }
    NSString *identifier = NSStringFromClass(item.viewClass);
    ICTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[item.viewClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if ([item isKindOfClass:[ICTableViewItem class]]) {
        [cell setObject:item];
    }
    
    if (tableView == self.tableView1 && self.type == 1) {
        ((EFCouponViewCell *)cell).selectBlock = ^(NSString* id){
            for (NSObject *object in self.dataArray1) {
                EFCouponViewItem* item = (EFCouponViewItem*)object;
                if([id isEqualToString:item.id]){
                    item.selected = true;
                }else{
                    item.selected = false;
                }
            }
            [self.tableView1 reloadData];
            [[NSNotificationCenter defaultCenter] postNotificationName:IC_CHANGE_COUPON_NOTIFICATION object:id];
            [self.navigationController popViewControllerAnimated:YES];
        };
    }
    
    
    
    
    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = self.scrollView.contentOffset.x/kScreenWidth;
    [self.segmentView setSeletIndex:index];
}


- (UITableView *)tableView1{
    if (!_tableView1) {
        _tableView1 = [self getATableView];
    }
    return _tableView1;
}

- (UITableView *)tableView2{
    if (!_tableView2) {
        _tableView2 = [self getATableView];
        _tableView2.left = kScreenWidth;
    }
    return _tableView2;
}


- (UITableView *)getATableView{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight -32.f)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.contentInset = UIEdgeInsetsMake(15.f, 0, 0, 0);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];;
    return tableView;
}

-(EFCouponSegmentView*)segmentView{
    if(_segmentView == NULL){
        _segmentView = [[EFCouponSegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 32)];
    }
    return _segmentView;
}


- (UIScrollView *)scrollView{
    if(self.type == 0)
    {
        if (!_scrollView) {
            _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 32, kScreenWidth, kScreenHeight - kStatusBarHeight - 32)];
            _scrollView.contentSize = CGSizeMake(kScreenWidth * 2, _scrollView.height);
            _scrollView.pagingEnabled = YES;
            _scrollView.delegate = self;
        }
        return _scrollView;
    }
    else
    {
        if (!_scrollView) {
            _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 32, kScreenWidth, kScreenHeight - kStatusBarHeight - 32)];
            _scrollView.contentSize = CGSizeMake(kScreenWidth, _scrollView.height);
            _scrollView.pagingEnabled = YES;
            _scrollView.delegate = self;
        }
        return _scrollView;
    }
}



@end
